<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * getVNDateTime()
 *
 * @return
 */
if ( ! function_exists('getVNDateTime'))
{
	function getVNDateTime($datetime,$gio=false)
	{
		$date = '';
		if(!empty($datetime)){
			if($gio == true){
                $date = date('d/m/Y H:i:s', strtotime($datetime));
			}else{
				$date = date('d/m/Y', strtotime($datetime));
			}
		}
		return $date;
	}
}

/**
 * getNow()
 *
 * @return
 */
if ( ! function_exists('getNow'))
{
	function getNow()
	{
		return date(DATETIME_FORMAT_DB);
	}
}
/****************************	BEGIN: FILE HELPER ***************************/
if ( ! function_exists('get_file_extension'))
{
	function get_file_extension($file_name){
		$extension = '';
		if(!empty($file_name)){
			$parts = pathinfo($file_name);
			$extension  = $parts ['extension'];
		}
		return $extension;
	}
}

if ( ! function_exists('check_exists_create'))
{
	function check_exists_create($dir_name, &$file_name, &$extension='')
	{
		$parts = pathinfo($file_name);
		$extension  = $parts ['extension'];
		$file_name = basename($file_name,".$extension");
		// Should be 9 cases
		$is_existed = 	file_exists($dir_name.$file_name.".$extension") || 
					file_exists($dir_name.$file_name.strtolower(".$extension")) ||
					file_exists($dir_name.$file_name.strtoupper(".$extension"));
		if($is_existed){
			$suffix = date(DATETIME_FORMAT_HYPHEN);
			$file_name = $file_name."_".$suffix;
		}
		$file_name  = "$file_name.$extension";
		return $is_existed;
	}
}

if ( ! function_exists('get_dirname_by_ext'))
{
	function get_dirname_by_ext($file_name)
	{
		$dirname = UPLOAD_DIR;
		$parts = pathinfo($file_name);
		$extension  = strtolower($parts['extension']);
		switch($extension){
			case 'flv':
				$dirname = VIDEO_DIR;
				break;
			case 'mp3':
				$dirname = AUDIO_DIR;
				break;
			case 'doc':
			case 'docx':
			case 'xls':
			case 'xlsx':
			case 'ppt':
			case 'pptx':
			case 'pdf':
				$dirname = DOCUMENT_DIR;
				break;
			case 'rar':
			case 'zip':
				$dirname = ZIP_DIR;
				break;
		}
		return $dirname;
	}
}

if ( ! function_exists('get_icon'))
{
	function get_icon($file_name)
	{
		$icon = "";
		$parts = pathinfo($file_name);
		$extension  = strtolower($parts['extension']);
		switch($extension){
			case 'doc':
			case 'docx':
				$icon = "DOC-48.png";
				break;
			case 'xls':
			case 'xlsx':
				$icon = "EXL-48.png";
				break;
			case 'img':
				$icon = "IMG-48.png";
				break;
			case 'mp3':
				$icon = "MP3-48.png";
				break;
			case 'flv':
				$icon = "FLV-48.png";
				break;
			case 'pdf':
				$icon = "PDF-48.png";
				break;
			case 'ppt':
			case 'pptx':
				$icon = "PPT-48.png";
				break;
			case 'rar':
			case 'zip':
				$icon = "ZIP-48.png";
				break;
			default:
				$icon = "other-48.PNG";
				break;
		}
		return $icon;
	}
}
/****************************	END: FILE HELPER ***************************/

/****************************	BEGIN: DEBUG HELPER ***************************/
if ( ! function_exists('last_query'))
{
	function last_query($exit=false)
	{
		$CI =& get_instance();
		echo $CI ->db->last_query();
		if($exit){
			exit();
		}
	}
}

if ( ! function_exists('pr'))
{
	function pr($item, $exit=false)
	{
		echo "<pre/>";
		if(is_array($item) || is_object($item)){
			var_dump($item);
		} else {
			echo $item;
		}
		if($exit){
			exit();
		}
	}
}

if ( ! function_exists('var_dump_ret'))
{
	function var_dump_ret($mixed = null) {
	  ob_start();
	  var_dump($mixed);
	  $content = ob_get_contents();
	  ob_end_clean();
	  return $content;
	}
}
/****************************	END: DEBUG HELPER ***************************/


/****************************	BEGIN: URL HELPER ***************************/
if ( ! function_exists('full_url'))
{
	function full_url()
	{
		$query = $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '';
		$full_url = substr(base_url(),0,-1).uri_string(). $query; 
		return $full_url;
	}
}
/****************************	END: URL HELPER ***************************/

/****************************	END: IMAGE HELPER ***************************/
if ( ! function_exists('resize_image'))
{
	function resize_image($source_dir, $file_name, $width, $height, $des_dir)
	{
		require_once 'phpthumb/ThumbLib.inc.php';  
		$thumb = PhpThumbFactory::create($source_dir.$file_name);  
		$thumb->resize($width, $height);
		$thumb->save($des_dir.$file_name);
	}
}

if ( ! function_exists('crop_image'))
{
	function crop_image($dataUpload, $dir_soure, $file_name, $width_o, $height_o, $des_dir)
	{
		//pr($dataUpload);echo $dir_soure.'----'.$file_name.'-----'.$width.'----'.$height.'-----'.$des_dir;exit();
		if($dataUpload['is_image'] == true)
		{
			$imageArray[] = $dataUpload['file_name'];
			// Get the image and create a thumbnail
			$ham = "imagecreatefrom".$dataUpload['image_type'];
			$img = $ham($dir_soure.$file_name);
			//echo $ham;exit();
			//$img = imagecreatefrompng($dir_soure.$file_name);
			if (!$img) {
				//echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"];
				exit(0);
			}
			$width = imageSX($img);
			$height = imageSY($img);
			if (!$width || !$height) {
				echo "ERROR:Invalid width or height";
				exit(0);
			}
			// Build the thumbnail
			$target_width = $width_o;
			$target_height = $height_o;
			$target_ratio = $target_width / $target_height;
			$img_ratio = $width / $height;
			if ($target_ratio > $img_ratio) {
				$new_height = $target_height;
				$new_width = $img_ratio * $target_height;
			} else {
				$new_height = $target_width / $img_ratio;
				$new_width = $target_width;
			}
			if ($new_height > $target_height) {
				$new_height = $target_height;
			}
			if ($new_width > $target_width) {
				$new_height = $target_width;
			}
			$new_img = ImageCreateTrueColor($width_o, $height_o);
			$white = imagecolorallocate($new_img, 255, 255, 255);
			if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, $white)) {	// Fill the image black
				echo "ERROR:Could not fill new image";
				exit(0);
			}
			if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
				echo "ERROR:Could not resize image";
				exit(0);
			}
			if (!isset($_SESSION["file_info"])) {
				$_SESSION["file_info"] = array();
			}
			// Use a output buffering to load the image into a variable
			$ham = "image".$dataUpload['image_type'];
			$ham($new_img, $des_dir.$file_name);
			//imagepng($new_img, $des_dir.$file_name);
			// Get the image and create a thumbnail
		}
	}
}
/****************************	END: IMAGE HELPER ***************************/

if(!function_exists('paginate'))
{
    function paginate($ci_instance, $base_url, $total_rows = 100, $current_page = 1, $per_page=10, $suffix=''){
        if($ci_instance != null)
        {
        	$ci_instance->load->library('pagination');
        	$config['cur_page'] = $current_page;					
        	$config['base_url'] = $base_url;
        	$config['total_rows'] = $total_rows;
        	$config['per_page'] = $per_page;
        	$config['page_query_string'] = false;
        	$config['uri_segment'] = 1;
            
            $config['next_tag_open'] = '<a class="hide">';
            $config['next_tag_close'] = '</a>';
            
        	$config['prev_tag_open'] = '<a class="hide">';
            $config['prev_tag_close'] = '</a>';
        	$config['last_link'] = '';
        	$config['cur_tag_open'] = '<strong>';
        	$config['cur_tag_close'] = '</strong>';
			
			$config['first_link'] = '';
			$config['last_link'] = '';
			$config['num_links'] = 5;
			$config['prev_link'] = '&lt;&lt; PREV';
			$config['next_link'] = 'NEXT &gt;&gt;';
            
            $config['first_tag_open'] = '<div class="hide">';
            $config['first_tag_close'] = '</div>';
            $config['last_tag_open'] = '<div class="hide">';
            $config['last_tag_close'] = '</div>';
        	$ci_instance->pagination->initialize($config);
        			
        	$data['paginator'] = $ci_instance->pagination->create_links();
            $patterns = '/html(\/?)(\d*)"/';
            $replacements = '0$2.html"';
            $data['paginator'] = preg_replace($patterns, $replacements, $data['paginator']);
            $ci_instance->load->vars($data);
        }
    }
}
